//
//  EDSCommonTabView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/7/12.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "EDSCommonTabView.h"

@interface EDSCommonTabView ()
//@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) NSMutableArray *buttons;

@property (nonatomic, strong) CALayer *anLine;

@property (nonatomic, strong) CALayer *bottomLine;

@property (nonatomic, strong) EDSCommonTabModel *model;

@end

@implementation EDSCommonTabView

- (instancetype)initWithFrame:(CGRect)frame model:(nonnull EDSCommonTabModel *)model
{
//    self.titles = titles;
    self.model = model;
    return [self initWithFrame:frame];
}

- (void)initWithVar
{
    _buttons = [[NSMutableArray alloc] initWithCapacity:0];
}

- (void)initWithSubView
{
    // [UIColor colorWithHexString:@"#999999"]
    // [UIColor colorWithHexString:@"#1A1A1A"]
    // [UIFont systemFontOfSize:15]
    
    self.backgroundColor = [UIColor whiteColor];
    CGRect frame = CGRectMake(0, self.height - 1, self.width, 1);
    _bottomLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#E6E6E6"]];
    
    float width = 0;//kScreenWidth / 3.0;
//    if (self.titles.count > 3)
//    {
        width = kScreenWidth / self.model.titles.count;
//    }
    frame = CGRectMake(0, 0, width, self.height - 3);
    for (int i=0; i<self.model.titles.count; i++)
    {
        NSString *title = self.model.titles[i];
        frame.origin.x = width * i;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        button.titleLabel.font = self.model.norTextFont;
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:self.model.norTextColor forState:UIControlStateNormal];
        [button setTitleColor:self.model.selTextColor forState:UIControlStateSelected];
        [self addSubview:button];
        [button addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
        [self.buttons addObject:button];
        if (i == 0)
        {
            button.titleLabel.font = self.model.selTextFont;
            button.selected = YES;
        }
    }
    
    NSString *str = self.model.titles.firstObject;
    
    float anwidth = [str widthForFont:[UIFont systemFontOfSize:15]];
//    if (self.titles.count > 3)
//    {
//        anwidth = 50;
//    }
    frame = CGRectMake(0, self.height - 3, anwidth, 3);
    _anLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#3399FF"]];
    _anLine.centerX = ((UIButton *)self.buttons.firstObject).centerX;
}

- (void)resetUI
{
    for (UIButton *button in self.buttons)
    {
        button.selected = NO;
        button.titleLabel.font = self.model.norTextFont;
    }
    UIButton *btn = self.buttons.firstObject;
    btn.selected = YES;
    btn.titleLabel.font = self.model.selTextFont;
    self.anLine.centerX = btn.centerX;
}

- (void)clickedBtn:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    for (UIButton *button in self.buttons)
    {
        button.selected = NO;
    }
    btn.selected = YES;
    float width = [btn.titleLabel.text widthForFont:btn.titleLabel.font];
    self.anLine.width = width;
    [UIView animateWithDuration:0.23 animations:^{
        self.anLine.centerX = btn.centerX;
    }];
    
    if (self.clickedBtnBlock)
    {
        self.clickedBtnBlock(btn.titleLabel.text, btn.tag - 100);
    }
    
}

- (void)updateIndex:(NSInteger)index
{
    UIButton *btn = self.buttons[index];
    [self clickedBtn:btn];
}
@end
